package com.neulogistics.contrib.warehouse.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.warehouse.api.application.command.CreateWarehouseCommand;
import com.neulogistics.contrib.warehouse.api.application.command.TransferOrderCommand;
import com.neulogistics.contrib.warehouse.api.application.command.WarehouseIdCommand;
import com.neulogistics.contrib.warehouse.api.application.query.IWarehouseQueryService;
import com.neulogistics.contrib.warehouse.api.application.query.WarehouseQuery;
import com.neulogistics.contrib.warehouse.api.application.validator.annotation.ValidateAddress;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.WarehouseTreeViewModel;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.WarehouseViewModel;
import com.neulogistics.contrib.warehouse.domain.model.valueobject.Address;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "库房管理")
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    IMediator mediator;

    @Resource
    private IWarehouseQueryService warehouseQueryService;

    @ApiOperation("新增库房库房")
    @PostMapping("/create")
    public BaseResponse<String> createWarehouse(@RequestBody @Validated CreateWarehouseCommand command){
        // 已校验数据格式

        return mediator.send(command);
    }

    /**
     * 返回所给地址附近的中心库房，和分站库房
     * 以map形式返回，key为 center、substation
     * @param address
     * @return
     */
    @ApiOperation("根据地址查询附近的库房")
    @PostMapping("/getRecommendedWarehouse")
    public BaseResponse<Map<String, List>> getRecommendedWarehouse(
            @RequestBody @Validated @NotNull @ValidateAddress Address address){
        // 已校验数据格式
        // 查询
        Map<String, List> result = warehouseQueryService.getRecommendedWarehouse(address);

        return ResultUtils.success(result);
    }

    @ApiOperation("调度")
    @PostMapping("/transfer")
    public BaseResponse<Boolean> transfer(
            @RequestBody @Validated @NotNull TransferOrderCommand command){
        // 已校验数据格式

        return mediator.send(command);
    }

    @ApiOperation("根据id查询库房")
    @PostMapping("/get")
    public BaseResponse<WarehouseViewModel> get(@RequestBody @NotNull WarehouseIdCommand command){
        // 已校验数据格式
        WarehouseViewModel warehouseViewModel = warehouseQueryService.getById(command.warehouseId);

        return ResultUtils.success(warehouseViewModel);
    }

    @ApiOperation("查询所有库房名")
    @GetMapping("/list")
    public BaseResponse<List<WarehouseTreeViewModel>> list(){


        List<WarehouseTreeViewModel> viewModelList = warehouseQueryService.list();

        return ResultUtils.success(viewModelList);
    }

    @ApiOperation("分页查询库房")
    @PostMapping("/page")
    public BaseResponse<Page<WarehouseViewModel>> page(@RequestBody @Validated @NotNull WarehouseQuery warehouseQuery){


        Page<WarehouseViewModel> viewModelPage = warehouseQueryService.page(warehouseQuery);

        return ResultUtils.success(viewModelPage);
    }
}
